package com.lixin.smartclassroom.ui.home.tab4.fragment

import android.view.LayoutInflater
import android.view.ViewGroup
import com.drake.brv.utils.bindingAdapter
import com.drake.channel.receiveEvent
import com.drake.net.utils.scopeNet
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.api.childyanxuecourselist
import com.lixin.smartclassroom.api.mychildzhktlist
import com.lixin.smartclassroom.api.yanxuecourselist
import com.lixin.smartclassroom.bean.DataListBean
import com.lixin.smartclassroom.databinding.FragmentRecyclerCustomBinding
import com.lixin.smartclassroom.ext.child_id
import com.lixin.smartclassroom.ext.user_role
import com.lixin.smartclassroom.ui.home.adapter.courseMyAttend
import com.lixin.smartclassroom.ui.home.adapter.courseMyCreate
import com.lixin.smartclassroom.ui.home.adapter.courseRecommend
import com.lixin.smartclassroom.ui.home.adapter.messageList
import com.lixin.smartclassroom.ui.home.adapter.yanXueCenter
import com.lixinkeji.base.BaseFragment
import com.lixinkeji.extension.getIntArgument
import com.lixinkeji.extension.gone
import com.lixinkeji.extension.visible

/**
 * Description
 * Author: lwc
 * Date:2023/12/6 14:03
 */
class Tab4ChildFragment : BaseFragment<FragmentRecyclerCustomBinding>() {
    override fun getViewBinding(
        inflater: LayoutInflater, container: ViewGroup?
    ) = FragmentRecyclerCustomBinding.inflate(inflater)

    private lateinit var listBean: ArrayList<DataListBean>
    private var type = 0
    override fun iniView() {
        type = getIntArgument("type")
        binding.recycler.yanXueCenter()


        receiveEvent<String>("refreshClassList") {
            binding.refresh.refresh()
        }

        binding.apply {
            refresh.onRefresh {
                scopeNetLife {
                    listBean = when (requireContext().user_role) {
                        1 -> {
                            if (type == 3) {
                                yanxuecourselist(index, 10, type.toString())!!
                            } else {
                                if (requireContext().child_id.isEmpty()) {
                                    ArrayList()
                                } else {
                                    childyanxuecourselist(index, 10, requireContext().child_id)!!
                                }
                            }
                        }

                        else -> yanxuecourselist(index, 10, type.toString())!!
                    }

                    addData(listBean) {
                        if (binding.recycler.bindingAdapter.models?.size == 0) {
                            binding.state.showEmpty()
                            binding.recycler.gone()
                        } else {
                            binding.recycler.visible()
                        }
                        listBean.size == 10
                    }
                }
            }.autoRefresh()
        }
    }

    override fun onResume() {
        super.onResume()
        binding.refresh.autoRefresh()
    }
}